// ==UserScript==
// @name         飞猪跟价工具辅助
// @namespace    https://gitee.com/amutravel/fg/tree/master/comparePrice
// @version      2020.12.25
// @description  飞猪跟价工具辅助脚本，用来更新 Cookies，其实没有这个脚本工具一样可以用，只是操作稍稍麻烦一点点。手工更新Cookies一样可以的
// @author       ╮(╯▽╰)╭
// @match        http://localhost:8080/
// @match        http://fp.lxfx.xyz:9010/fg
// @match        *://traveldetail.fliggy.com/item.htm?*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_addValueChangeListener
// @grant        GM_registerMenuCommand
// @grant        GM_notification
// @grant        unsafeWindow
// ==/UserScript==
;

if(location.host == "localhost:8080" || location.host == "fp.lxfx.xyz:9010"){

  GM_addValueChangeListener('FG_COOKIES', (name, old_value, new_value, remote)=>{
    if(new_value){
      try {
        unsafeWindow.fgApp.$bus.$emit('autoUpdateCookies', new_value)
      } catch (error) {
        console.log('[Tampermonkey updateCookies error:]', error)
      }
      GM_notification({
        title: "跟价工具更新Cookies",
        text: new_value,
        highlight: true,
        timeout: 1500,
      })
    }

  })

}
else if(location.host == "traveldetail.fliggy.com"){

  /*! js-cookie v3.0.0-beta.0 | MIT */
  !function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e=e||self,function(){var t=e.Cookies,o=e.Cookies=n();o.noConflict=function(){return e.Cookies=t,o}}())}(this,function(){"use strict";function e(){for(var e={},n=0;n<arguments.length;n++){var t=arguments[n];for(var o in t)e[o]=t[o]}return e}function n(e){return e.replace(/(%[\dA-F]{2})+/gi,decodeURIComponent)}return function t(o){function r(n,t,r){if("undefined"!=typeof document){"number"==typeof(r=e(i.defaults,r)).expires&&(r.expires=new Date(1*new Date+864e5*r.expires)),r.expires&&(r.expires=r.expires.toUTCString()),t=o.write?o.write(t,n):encodeURIComponent(String(t)).replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g,decodeURIComponent),n=encodeURIComponent(String(n)).replace(/%(23|24|26|2B|5E|60|7C)/g,decodeURIComponent).replace(/[()]/g,escape);var c="";for(var f in r)r[f]&&(c+="; "+f,!0!==r[f]&&(c+="="+r[f].split(";")[0]));return document.cookie=n+"="+t+c}}var i={defaults:{path:"/"},set:r,get:function(e){if("undefined"!=typeof document&&(!arguments.length||e)){for(var t=document.cookie?document.cookie.split("; "):[],r={},i=0;i<t.length;i++){var c=t[i].split("="),f=c.slice(1).join("=");'"'===f.charAt(0)&&(f=f.slice(1,-1));try{var u=n(c[0]);if(r[u]=(o.read||o)(f,u)||n(f),e===u)break}catch(e){}}return e?r[e]:r}},remove:function(n,t){r(n,"",e(t,{expires:-1}))},withConverter:t};return i}(function(){})});


  GM_registerMenuCommand('更新 Cookies', ()=>{
    GM_setValue('FG_COOKIES', '')
    setTimeout(() => {
      GM_setValue('FG_COOKIES', document.cookie)
    }, 500);
  }, 'U')

  //清除所有cookie函数
  GM_registerMenuCommand('清除所有 Cookie', ()=>{
    let keys = document.cookie.match(/[^ =;]+(?=\=)/g);
    keys.forEach(key=>{
      Cookies.remove(key)
    })
  }, 'C')

}